<?php

class Cookie {
	
	var $__expire;
	var $__path;
	var $__domain;
	var $__secure;
	var $__httponly;
	var $__name;
	var $__encrypt;
	var $__encryptor;
	
	function __construct()
   	{
		$this->__expire = Config::get('cookie_expire');
		$this->__path = Config::get('cookie_path');
		$this->__domain = Config::get('cookie_domain');
		$this->__secure = Config::get('cookie_secure');
   		$this->__encryptor = new Encryption();
   	}
    
   	function setName($name) {
   		$this->__name = $name;
   	}
   	
   	function setExpire($value) {
   		$this->__expire = $value;
   	}

	function setPath($path) {
   		$this->__path = $path;
   	}
   	
	function setDomain($domain) {
   		$this->__domain = $domain;
   	}
   	
	function setSecure($isSecure) {
   		$this->__secure = $isSecure;
   	}
   	
	function setHttpOnly($isHttpOnly) {
   		$this->__httponly = $isHttpOnly;
   	}
   	
   	function setEncrypt($value) {
   		$this->__encrypt = value;
   	}

	function get($name, $encrypt = false) {
		$cookie = $_COOKIE[$name] != null ? $_COOKIE[$name] : null;
		if ($cookie) {
			if ($this->__encrypt || $encrypt) {
				return $this->__encryptor->decrypt($cookie);
			} else {
				return $cookie;
			}
		}
		return null;
	}	
	
	function set($name, $value, $encrypt = false) {
		if ($this->__encrypt || $encrypt) {
			$value = $this->__encryptor->encrypt($value);
		}
		setcookie(
			$name, 
			$value, 
			$this->__expire == 0 ? 0 : time() + $this->__expire,
			$this->__path,
			null,
			$this->__secure,
			$this->__httponly
		);
	}
	
	function clear($name) {
		setcookie($name, null, time() - 3600);
		unset($_COOKIE[$name]);
		
	}
	
}

?>